package cn.broadsense.system.service.impl;

import cn.broadsense.system.domain.SysNotice;
import cn.broadsense.system.domain.SysUserNotice;
import cn.broadsense.system.mapper.SysNoticeMapper;
import cn.broadsense.system.service.ISysNoticeService;
import cn.broadsense.system.service.ISysUserNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService {
    @Autowired
    private SysNoticeMapper noticeMapper;
    @Autowired
    private ISysUserNoticeService sysUserNoticeService;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId) {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    @Transactional
    public void insertNotice(SysNotice notice) {
        noticeMapper.insertNotice(notice);
        if (!notice.getUserId().equals(0L)) {
            SysUserNotice userNotice = new SysUserNotice();
            userNotice.setNoticeId(notice.getNoticeId());
            userNotice.setStatus(0);
            userNotice.setUserId(notice.getUserId());
            sysUserNoticeService.save(userNotice);
        }
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice) {
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }

    /**
     * 根据用户ID获取公告信息
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public List<SysNotice> selectNoticeListByUserId(Long userId) {
        return noticeMapper.selectNoticeListByUserId(userId);
    }

    @Override
    public List<SysNotice> getList(SysNotice sysNotice) {
        return noticeMapper.getList(sysNotice);
    }
}
